#include "SocketServer.h"
#include <iostream>

using namespace std;

SocketServer::SocketServer(){}

SocketServer::SocketServer(int port) : Socket(port)
{
	init(port);
}

SocketServer::~SocketServer(){}

SocketServer SocketServer::operator=(int sock)
{
  _sock = sock;
  return *(this);
}

void SocketServer::init(int port)
{
	_port = port;
	_sock = socket(AF_INET, SOCK_STREAM, 0);
	bzero((char *) &_addr, sizeof(_addr));
	_addr.sin_family = AF_INET;
	_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	_addr.sin_port = htons(_port);  
	bind(_sock, (struct sockaddr *) &_addr, sizeof(_addr));
	if(listen(_sock, 5) != 0)
		cout << "Error : coulnd't listen" << endl;
}

int SocketServer::accept_connection()
{
  socklen_t size = sizeof(_addr);
  int sock = accept(_sock, (struct sockaddr *) &_addr, &size);
  if(sock < 0)
	perror("Accept error : ");
  return sock;
}
